package api.service;

import api.model.ApiCollect;
import api.model.ApiCollectColumn;
import api.model.ApiCollectJoin;
import m.common.model.util.ModelCheckUtil;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.common.service.Service;
import m.system.db.TransactionManager;
import m.system.exception.MException;
import m.system.util.GenerateID;
import m.system.util.StringUtil;

import java.util.List;

public class ApiCollectService extends Service {

    public String save(ApiCollect model) throws Exception {
        String msg;
        TransactionManager tm=new TransactionManager();
        try {
            tm.begin(model.getOid());
            ModelCheckUtil.checkNotNull(model, "description","title","alias","source");
            ModelCheckUtil.checkUniqueCombine(model,new String[]{"description"});
            if("table".equals(model.getSource())){
                ModelCheckUtil.checkNotNull(model, "table.oid");
                model.setCollect(null);
            }else{
                ModelCheckUtil.checkNotNull(model, "collect.oid");
                model.setTable(null);
            }
            if(!model.getAlias().matches("[a-z_0-9]*")) throw new MException(this.getClass(),"别名只能包含小写字母数字和下划线");
            if(StringUtil.isSpace(model.getOid())){
                model.setOid(GenerateID.generatePrimaryKey());
                ModelUpdateUtil.insertModel(model);
                msg="保存成功";
            }else{
                List<ApiCollectJoin> list = ModelQueryList.getModelList(ApiCollectJoin.class,new String[]{"alias"},null,
                    QueryCondition.eq("collect.oid",model.getOid()));
                for(ApiCollectJoin join : list){
                    if(join.getAlias().equals(model.getAlias())){
                        throw new MException(this.getClass(),"链接表已存在别名"+model.getAlias());
                    }
                }
                ModelUpdateUtil.updateModel(model, "description","title","subTitle","source","table.oid","collect.oid","alias","showIndexColumn",
                    "sqlCondition","sqlOrder","sqlHaving","scol","slength","functions");
                msg="修改成功";
            }
            tm.commit();
        }catch (Exception e){
            tm.rollback();
            throw e;
        }
        return msg;
    }
    public String saveJoin(ApiCollectJoin model) throws Exception {
        String msg;
        TransactionManager tm=new TransactionManager();
        try {
            tm.begin(model.getOid());
            ModelCheckUtil.checkNotNull(model, "alias","source");
            if(!model.getAlias().matches("[a-z_0-9]*")) throw new MException(this.getClass(),"别名只能包含小写字母数字和下划线");
            if("table".equals(model.getSource())){
                ModelCheckUtil.checkNotNull(model, "table.oid");
                model.setCollect(null);
            }else{
                ModelCheckUtil.checkNotNull(model, "collect.oid");
                model.setTable(null);
            }
            model.setApiCollect(ModelQueryList.getModel(model.getApiCollect(),"*"));
            if(model.getAlias().equals(model.getApiCollect().getAlias())) throw new MException(this.getClass(),"链接表别名不能与汇总表别名相同");
            if(StringUtil.isSpace(model.getOid())){
                model.setOid(GenerateID.generatePrimaryKey());
                ModelUpdateUtil.insertModel(model);
                msg="保存成功";
            }else{
                ModelUpdateUtil.updateModel(model, "sort","joinType","source","table.oid","collect.oid","alias","joinOn");
                msg="修改成功";
            }
            tm.commit();
        }catch (Exception e){
            tm.rollback();
            throw e;
        }
        return msg;
    }
    public String saveColumn(ApiCollectColumn model) throws Exception {
        String msg;
        TransactionManager tm=new TransactionManager();
        try {
            tm.begin(model.getOid());
            ModelCheckUtil.checkNotNull(model, "field","alias","width","collect.oid","type");
            ModelCheckUtil.checkUniqueCombine(model,new String[]{"alias","collect.oid"},"列别名已存在");
            if(!model.getAlias().matches("[a-z_0-9]*")) throw new MException(this.getClass(),"别名只能包含小写字母数字和下划线");
            if("DATE".equals(model.getType())){
                model.setLineClamp(2);
                model.setNumberFormat("");
                model.setIsCount("N");
                model.setDict("");
            }else if("NUMBER".equals(model.getType())){
                model.setLineClamp(2);
                model.setDateFormat("");
                model.setIsOrder("N");
                model.setDict("");
            }else{
                model.setDateFormat("");
                model.setNumberFormat("");
                model.setIsCount("N");
            }
            if(!"N".equals(model.getIsCount())){
                model.setIsFilter("N");
            }
            if(StringUtil.isSpace(model.getOid())){
                model.setOid(GenerateID.generatePrimaryKey());
                ModelUpdateUtil.insertModel(model);
                msg="保存成功";
            }else{
                ModelUpdateUtil.updateModel(model, "title","groupTitle","field","alias","width","sort","dict",
                    "type","numberFormat","dateFormat","lineClamp","isShow","isOrder","isCount","isFilter","textAlign");
                msg="修改成功";
            }
            tm.commit();
        }catch (Exception e){
            tm.rollback();
            throw e;
        }
        return msg;
    }
}
